﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Nancy;
using System.IO;

namespace ConfigServer
{
    public class HomeModule:NancyModule
    {
        public HomeModule()
        {
            Get[@"/"] = _ =>
            {
                var model = new { title = "配置管理器", body = "<h2>配置管理器</h2>" };
                
                return View["home", model];
            };

            Get[@"/config/{name}/{profile}/{label}"] = _ =>
            {
                string name = _.name;
                string profile = _.profile;
                string label = _.label;

                string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config", label, name + "-" + profile + ".yml");

                string yml = File.ReadAllText(filePath);

                SharpYaml.Serialization.Serializer sr = new SharpYaml.Serialization.Serializer();
                var obj = sr.Deserialize(yml,null);

                var json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);

                return this.Response.AsText(json,"application/json");
            };

            
        }
    }
}